/**
 * 培训计划详情
 */
<template>
    <el-form v-loading="loading" :model="formData" :rules="currentFormRules" ref="formRef" label-width="120px" :scroll-to-error="true">
        <vp-card>
            <template #header>
                <vp-label zh="基本信息" en="Basic Info" />
            </template>
            <el-row>
                <vp-col :span="4">
                    <el-form-item prop="jhKs">
                    <template #label><vp-label zh="计划开始时间" en="Planned Start Time"/></template>
                        <el-date-picker
                            v-model="formData.jhKs"
                            value-format="YYYY-MM-DD HH:mm"
                            format="YYYY-MM-DD HH:mm"
                            time-format="HH:mm"
                            type="datetime"
                            v-if="isEditPage"
                            class="w100"
                        />
                        <span v-else>{{ formData.jhKs }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="jhJs">
                    <template #label><vp-label zh="计划结束时间" en="Planned End Time"/></template>
                        <el-date-picker
                            v-model="formData.jhJs"
                            value-format="YYYY-MM-DD HH:mm"
                            format="YYYY-MM-DD HH:mm"
                            time-format="HH:mm"
                            type="datetime"
                            v-if="isEditPage"
                            class="w100"
                        />
                        <span v-else>{{ formData.jhJs }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4" >
                    <el-form-item prop="lx">
                        <template #label><vp-label zh="计划类型" en="Plan Type"/></template>
                        <el-select 
                            v-model="formData.lx"  
                            v-if="isEditPage"
                        >
                            <el-option :value="10" label="年度计划"></el-option>
                            <el-option :value="20" label="临时"></el-option>
                        </el-select>
                        <span v-else>{{formData.lx === 10? '年度计划' : formData.lx === 20 ? '临时' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="pxbh">
                        <template #label><vp-label zh="培训编号" en="Training No." /></template>
                        <el-input v-model="formData.pxbh" v-if="isEditPage" disabled placeholder="系统自动生成" v-trim maxlength="100">
                        </el-input>
                        <span v-else>{{ formData.pxbh }}</span>
                    </el-form-item>
                </vp-col>
            </el-row>
            <el-row v-if="type !== 'add' && type !== 'edit'">
                <vp-col :span="4">
                    <el-form-item prop="sjKs">
                    <template #label><vp-label zh="实际开始时间" en="Actual Start Time"/></template>
                        <el-date-picker
                            v-model="formData.sjKs"
                            value-format="YYYY-MM-DD HH:mm"
                            format="YYYY-MM-DD HH:mm"
                            time-format="HH:mm"
                            type="datetime"
                            v-if="isEditPage"
                            class="w100"
                        />
                        <span v-else>{{ formData.sjKs }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="sjJs">
                    <template #label><vp-label zh="实际结束时间" en="Actual End Time"/></template>
                        <el-date-picker
                            v-model="formData.sjJs"
                            value-format="YYYY-MM-DD HH:mm"
                            format="YYYY-MM-DD HH:mm"
                            time-format="HH:mm"
                            type="datetime"
                            v-if="isEditPage"
                            class="w100"
                        />
                        <span v-else>{{ formData.sjJs }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="sjks">
                        <template #label><vp-label zh="实际课时" en="Actual Class Hour" /></template>
                        <el-input 
                            v-model="formData.sjks" 
                            v-if="isEditPage" 
                            @input="(val:string) => {formData.sjks = verifyNumberIntegerAndFloat(val) }" 
                            v-trim
                            maxlength="8"
                        >
                            <template #append>Hrs</template>
                        </el-input>
                        <span v-else>{{ formData.sjks || formData.sjks === 0 ? formData.sjks + 'Hrs' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="ksfbs">
                        <template #label><vp-label zh="课时费" en="Class Hour Fee" /></template>
                        <el-radio-group 
                            v-model="formData.ksfbs" 
                            v-if="isEditPage"
                        >
                            <el-radio :value="10">是</el-radio>
                            <el-radio :value="20">否</el-radio>
                        </el-radio-group>
                        <span v-else>{{ formData.ksfbs === 10 ? '是' : 
                                        formData.ksfbs === 20 ? '否' : '' }}</span>
                    </el-form-item>
                </vp-col>
            </el-row>
            <el-row>
                <vp-col :span="4">
                    <el-form-item prop="kcid">
                        <template #label><vp-label zh="课程编号" en="Course No."/></template>
                        <el-input v-model="formData.kcbh" readonly v-if="isEditPage">
                            <template #append>
                                <el-button @click="handleChooseCourse"><SvgIcon name="ele-Search" /></el-button>
                            </template>
                        </el-input>
                        <el-link v-else @click="handleViewCourse(formData.kcid)">{{ formData.kcbh }}</el-link>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="kcmcZw">
                        <template #label><vp-label zh="课程名称(中)" en="Course Name (ZH)"/></template>
                        <el-input v-model="formData.kcmcZw" disabled v-if="isEditPage">
                        </el-input>
                        <span v-else>{{ formData.kcmcZw }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="kcmcYw">
                        <template #label><vp-label zh="课程名称(英)" en="Course Name (EN)"/></template>
                        <el-input v-model="formData.kcmcZw" disabled v-if="isEditPage">
                        </el-input>
                        <span v-else>{{ formData.kcmcYw }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="fsbs">
                        <template #label><vp-label zh="是否复训" en="Retraining" /></template>
                        <el-radio-group 
                            v-model="formData.fsbs" 
                            v-if="isEditPage"
                            @change="handleChangeFsbs"
                        >
                            <el-radio :value="10">初训</el-radio>
                            <el-radio :value="20">复训</el-radio>
                        </el-radio-group>
                        <span v-else>{{ formData.fsbs === 10 ? '初训' : 
                                        formData.fsbs === 20 ? '复训' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="ks">
                        <template #label><vp-label zh="课时" en="Class Hour" /></template>
                        <el-input 
                            v-model="formData.ks" 
                            v-if="isEditPage" 
                            @input="(val:string) => {formData.ks = verifyNumberIntegerAndFloat(val) }" 
                            v-trim
                            maxlength="8"
                        >
                            <template #append>Hrs</template>
                        </el-input>
                        <span v-else>{{ formData.ks || formData.ks === 0 ? formData.ks + 'Hrs' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="xt">
                        <template #label><vp-label zh="学天" en="Days" /></template>
                        <el-input 
                            v-model="formData.xt" 
                            v-if="isEditPage" 
                            @input="(val:string) => {formData.xt = verifyNumberIntegerAndFloat(val) }" 
                            v-trim
                            maxlength="8"
                        >
                            <template #append>天</template>
                        </el-input>
                        <span v-else>{{ formData.xt ? formData.xt + '天' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="pxxs">
                        <template #label><vp-label zh="培训形式" en="Training Format" /></template>
                        <VpDictSelect 
                            :lxid="602" 
                            :selectedId="formData.pxxs"
                            v-if="isEditPage"
                            @dictSelected="dicts => { formData.pxxs = dicts }" 
                            class="w100" 
                        />
                        <span v-else>{{ formData.pxxs }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="ksxs">
                        <template #label><vp-label zh="考试形式" en="Exam Format" /></template>
                        <VpDictSelect 
                            :lxid="603" 
                            :selectedId="formData.ksxs"
                            v-if="isEditPage"
                            @dictSelected="dicts => { formData.ksxs = dicts }" 
                            class="w100" 
                        />
                        <span v-else>{{ formData.ksxs }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="jydaid">
                        <template #label><vp-label zh="教员" en="Trainer" /></template>
                        <el-input readonly v-model="formData.jyxm" maxlength="100" v-if="isEditPage">
                            <template #append>
                                <el-button @click="handleChooseTrainer"><SvgIcon name="ele-Search"/></el-button>
                            </template>
                        </el-input>
                        <span v-else>{{ formData.jyxm }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="zbdw">
                        <template #label><vp-label zh="主办单位" en="Host Org." /></template>
                        <el-input v-model="formData.zbdw" v-if="isEditPage" v-trim maxlength="100">
                        </el-input>
                        <span v-else>{{ formData.zbdw }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="pxjg">
                        <template #label><vp-label zh="培训机构" en="Training Org." /></template>
                        <el-input v-model="formData.pxjg" v-if="isEditPage" v-trim maxlength="100">
                        </el-input>
                        <span v-else>{{ formData.pxjg }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="pxdd">
                        <template #label><vp-label zh="培训地点" en="Training Location" /></template>
                        <el-input v-model="formData.pxdd" v-if="isEditPage" v-trim maxlength="100">
                        </el-input>
                        <span v-else>{{ formData.pxdd }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="sfbzbs">
                        <template #label><vp-label zh="是否颁证" en="Certificate Issuance" /></template>
                        <el-radio-group 
                            v-model="formData.sfbzbs" 
                            v-if="isEditPage"
                        >
                            <el-radio :value="10">是</el-radio>
                            <el-radio :value="20">否</el-radio>
                        </el-radio-group>
                        <span v-else>{{ formData.sfbzbs === 10 ? '是' : 
                                        formData.sfbzbs === 20 ? '否' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="jcffbs">
                        <template #label><vp-label zh="教材发放" en="Training Materials" /></template>
                        <el-radio-group 
                            v-model="formData.jcffbs" 
                            v-if="isEditPage"
                        >
                            <el-radio :value="10">是</el-radio>
                            <el-radio :value="20">否</el-radio>
                        </el-radio-group>
                        <span v-else>{{ formData.jcffbs === 10 ? '是' : 
                                        formData.jcffbs === 20 ? '否' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="zxxxbs">
                        <template #label><vp-label zh="在线学习" en="Online Learning" /></template>
                        <el-radio-group 
                            v-model="formData.zxxxbs" 
                            v-if="isEditPage"
                        >
                            <el-radio :value="10">是</el-radio>
                            <el-radio :value="20">否</el-radio>
                        </el-radio-group>
                        <span v-else>{{ formData.zxxxbs === 10 ? '是' : 
                                        formData.zxxxbs === 20 ? '否' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="4">
                    <el-form-item prop="zxksbs">
                        <template #label><vp-label zh="在线考试" en="Online Exam" /></template>
                        <el-radio-group 
                            v-model="formData.zxksbs" 
                            v-if="isEditPage"
                        >
                            <el-radio :value="10">是</el-radio>
                            <el-radio :value="20">否</el-radio>
                        </el-radio-group>
                        <span v-else>{{ formData.zxksbs === 10 ? '是' : 
                                        formData.zxksbs === 20 ? '否' : '' }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="1" v-if="type !== 'editResult' && type !== 'close'">
                    <el-form-item prop="requiredPersons">
                        <template #label><vp-label zh="参训人员" en="Participants" /></template>
                        <el-input readonly v-model="trainingParticipantDesc" v-if="isEditPage">
                            <template #append>
                                <el-button @click="handleChoosePlannedParticipants"><SvgIcon name="ele-Search"/></el-button>
                            </template>
                        </el-input>
                        <span v-else>{{ trainingParticipantDesc }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="1" v-if="type === 'editResult' || type === 'close' || type === 'view'">
                    <el-form-item prop="actualPersons">
                        <template #label><vp-label zh="培训详情" en="Deatil" /></template>
                        <vp-table :data="formData.actualPersons">
                            <el-table-column type="index" align="center" label="#"></el-table-column>
                            <el-table-column min-width="90" show-overflow-tooltip>
                                <template #header><vp-label zh="人员编号" en="Person No."/></template>
                                <template #default="scope">
                                    <el-link @click="handleViewPersonnelFile(scope.row.rydaid)">{{ scope.row.dabh }}</el-link>
                                </template>
                            </el-table-column>
                            <el-table-column prop="ryxm" min-width="80" show-overflow-tooltip>
                                <template #header><vp-label zh="姓名" en="Name"/></template>
                            </el-table-column>
                            <el-table-column prop="bm" min-width="120" show-overflow-tooltip>
                                <template #header><vp-label zh="部门" en="Department"/></template>
                            </el-table-column>
                            <el-table-column prop="jg" width="170" show-overflow-tooltip>
                                <template #header><vp-label zh="考核结论" en="Result"/></template>
                                <template #default="scope">
                                    <el-form-item :prop="`actualPersons[${scope.$index}].jg`" :rules="type === 'close' ? [validateUtils.notNull] : []" label-width="auto" :inline-message="true" v-if="isEditPage">
                                        <el-radio-group v-model="scope.row.jg">
                                            <el-radio :value="10" class="mr10">合格</el-radio>
                                            <el-radio :value="20">不合格</el-radio>
                                        </el-radio-group>
                                    </el-form-item>
                                    <span v-else>{{ scope.row.jg === 10 ? '合格' : 
                                                    scope.row.jg === 20 ? '不合格' : '' }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column min-width="100" show-overflow-tooltip>
                                <template #header><vp-label zh="成绩" en="Score"/></template>
                                <template #default="scope">
                                    <el-form-item :prop="`actualPersons[${scope.$index}].cj`" label-width="auto" v-if="isEditPage">
                                        <el-input v-model="scope.row.cj" @input="(val:string) => {scope.row.cj = verifyNumberIntegerAndFloat1(val) }" maxlength="6" />
                                    </el-form-item>
                                    <span v-else>{{ scope.row.cj }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column min-width="120" show-overflow-tooltip>
                                <template #header><vp-label zh="证书编号" en="Certificate No."/></template>
                                <template #default="scope">
                                    <el-form-item :prop="`actualPersons[${scope.$index}].zsbh`" label-width="auto" v-if="isEditPage">
                                        <el-input v-model="scope.row.zsbh" v-trim maxlength="100" />
                                    </el-form-item>
                                    <span v-else>{{ scope.row.zsbh }}</span>
                                </template>
                            </el-table-column>
                            <el-table-column min-width="160" show-overflow-tooltip>
                                <template #header><vp-label zh="附件" en="Attachment"/></template>
                                <template #default="scope">
                                    <RowAttachment 
                                        :attachmentList="scope.row.attachments" 
                                        :readOnly="!isEditPage"
                                        @attachmentListEmit="scope.row.attachments = $event"
                                    />
                                </template>
                            </el-table-column>
                            <el-table-column align="center" width="60" v-if="isEditPage">
                                <template #header>
                                    <el-button circle @click="handleChooseActualParticipants()"><SvgIcon name="ele-Plus"></SvgIcon></el-button>
                                </template>
                                <template #default="scope">
                                    <el-button circle @click="handleDeleteActualParticipant(scope.$index)"><SvgIcon name="ele-Minus"></SvgIcon></el-button>
                                </template>
                            </el-table-column>
                        </vp-table>
                    </el-form-item>
                </vp-col>
            </el-row>
            <el-row>
                <vp-col :span="1">
                    <el-form-item prop="fbt">
                        <template #label><vp-label zh="副标题" en="Subtitle"/></template>
                        <el-input 
                            type="textarea" 
                            v-model="formData.fbt" 
                            maxlength="100" 
                            :autosize="{ minRows: 2}"
                            v-if="isEditPage"
                        >
                        </el-input>
                        <span v-else >{{ formData.fbt }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="1">
                    <el-form-item prop="pxdx">
                        <template #label><vp-label zh="培训对象" en="Target Audience"/></template>
                        <el-input 
                            type="textarea" 
                            v-model="formData.pxdx" 
                            maxlength="1000" 
                            :autosize="{ minRows: 4}"
                            v-if="isEditPage"
                        >
                        </el-input>
                        <span v-else >{{ formData.pxdx }}</span>
                    </el-form-item>
                </vp-col>
                <vp-col :span="1">
                    <el-form-item prop="bz">
                        <template #label><vp-label zh="其它说明" en="Other Remark"/></template>
                        <el-input 
                            type="textarea" 
                            v-model="formData.bz" 
                            maxlength="1000" 
                            :autosize="{ minRows: 4}"
                            v-if="isEditPage"
                        >
                        </el-input>
                        <span v-else >{{ formData.bz }}</span>
                    </el-form-item>
                </vp-col>
            </el-row>
        </vp-card>

        <!-- 课程单选页面 -->
		<VpCourseDialog ref="vpCourseDialogRef" @confirm="handleCourseConfirmed"></VpCourseDialog>
        <!-- 人员选择页面 -->
        <VpUserDialog ref="vpUserDialogRef"></VpUserDialog>
    </el-form>
</template>
<script setup lang="ts" name="trainingPlan">
import { ref, defineAsyncComponent, computed } from 'vue';
import validateUtils from '/@/utils/validateUtils';
import { verifyNumberIntegerAndFloat, verifyNumberIntegerAndFloat1 } from '/@/utils/toolsValidate';
import VpDictSelect from '/@/components/vp/dict/VpDictSelect.vue';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';
import RowAttachment from '/@/components/vp/file/VpRowAttachment.vue';
import request from '/@/utils/request';

const VpCourseDialog = defineAsyncComponent(() => import('/@/components/vp/course/VpCourseDialog.vue'));
const VpUserDialog = defineAsyncComponent(() => import('/@/components/vp/user/VpUserDialog.vue'));

const emits = defineEmits(['formSubmited']);
const props = defineProps({
    // 页面类型
	type: {
		type: String,
		default: 'add',
	},
	// 加载状态
	loading: {
		type: Boolean,
		default: false,
	},
	// 开启验证
	enableValid: {
		type: Boolean,
		default: true,
	},
});
const formData = ref({
    id:"",
    zt:10,
    lx: null,
    pxbh:"",
    kcid:"",
    kcbh: "",
    kcmcZw: "",
    kcmcYw: "",
    fbt:"",
    jhKs:"",
    jhJs:"",
    jhKsrq:"",
    jhKssj:"",
    jhJsrq:"",
    jhJssj:"",
    fsbs: null,
    ks:"",
    xt:"",
    pxxs:"",
    ksxs:"",
    zbdw:"",
    pxjg:"",
    pxdd:"",
    sfbzbs:null,
    jcffbs:null,
    zxxxbs:null,
    zxksbs:null,
    jydaid:"",
    jyxm:"",
    pxdx:"",
    ycrs:null,
    bz:"",
    sjKs:"",
    sjJs:"",
    sjKsrq:"",
    sjKssj:"",
    sjJsrq:"",
    sjJssj:"",
    scrs:null,
    sjks:"",
    ksfbs:null,
    requiredPersons: [] as any[],
    actualPersons: [] as any[],
});
const formRules = {
    jhKs:[ validateUtils.notNull ],
    lx:[ validateUtils.notNull ],
    kcid:[ validateUtils.notNull ],
    fsbs:[ validateUtils.notNull ],
    pxxs:[ validateUtils.notNull ],
    ksxs:[ validateUtils.notNull ],
    zxxxbs:[ validateUtils.notNull ],
    zxksbs:[ validateUtils.notNull ],
    requiredPersons:[ validateUtils.notNull ],
    sjKs:[ validateUtils.notNull ],
    sjks:[ validateUtils.notNull ],
    ksfbs:[ validateUtils.notNull ],
    jydaid:[ validateUtils.notNull ],
}

// 页面验证规则
const currentFormRules = computed(() => {
    if (!props.enableValid) {
        return {};
    }
    if (isEditPage.value) {
        return formRules;
    }
    return {};
})

const formRef = ref();

// 是否是编辑页面
const isEditPage = computed(() => {
    return props.type === 'add' || props.type === 'edit' || props.type === 'issue' || props.type === 'editResult' || props.type === 'close';
});


// 清空数据
const initFormData = ()=>{
    formData.value = {
        id:"",
        zt:10,
        lx: null,
        pxbh:"",
        kcid:"",
        kcbh: "",
        kcmcZw: "",
        kcmcYw: "",
        fbt:"",
        jhKs:"",
        jhJs:"",
        jhKsrq:"",
        jhKssj:"",
        jhJsrq:"",
        jhJssj:"",
        fsbs: null,
        ks:"",
        xt:"",
        pxxs:"",
        ksxs:"",
        zbdw:"",
        pxjg:"",
        pxdd:"",
        sfbzbs:null,
        jcffbs:null,
        zxxxbs:null,
        zxksbs:null,
        jydaid:"",
        jyxm:"",
        pxdx:"",
        ycrs:null,
        bz:"",
        sjKs:"",
        sjJs:"",
        sjKsrq:"",
        sjKssj:"",
        sjJsrq:"",
        sjJssj:"",
        scrs:null,
        sjks:"",
        ksfbs:null,
        requiredPersons: [] as any[],
        actualPersons: [] as any[],
    }
}

// 回显数据
const setData = (data: any) => {
    initFormData();
    formData.value = {
        ...formData.value,
        ...data,
        jhKs: data.jhKsrq ? (data.jhKsrq + ' ' + data.jhKssj) : '',
        jhJs: data.jhJsrq ? (data.jhJsrq + ' ' + data.jhJssj) : '',
        sjKs: data.sjKsrq ? (data.sjKsrq + ' ' + data.sjKssj) : '',
        sjJs: data.sjJsrq ? (data.sjJsrq + ' ' + data.sjJssj) : '',
    };
}

/**
 * 获取数据
 */
const getData = (): Promise<any>=>{
    return new Promise((resolve, reject) => {
        formRef.value.validate((valid: boolean) => {
            const data = {
                ...formData.value,
                jhKsrq: (formData.value.jhKs || '').substring(0, 10),
                jhKssj: (formData.value.jhKs || '').substring(11, 16),
                jhJsrq: (formData.value.jhJs || '').substring(0, 10),
                jhJssj: (formData.value.jhJs || '').substring(11, 16),
                sjKsrq: (formData.value.sjKs || '').substring(0, 10),
                sjKssj: (formData.value.sjKs || '').substring(11, 16),
                sjJsrq: (formData.value.sjJs || '').substring(0, 10),
                sjJssj: (formData.value.sjJs || '').substring(11, 16),
                ycrs: (formData.value.requiredPersons || []).length,
                scrs: (formData.value.actualPersons || []).length
            };
            if (valid) {
                if(formData.value.jhJs && formData.value.jhKs > formData.value.jhJs){
                    ElMessage.error('计划开始时间不能大于计划结束时间/The planned start date cannot be later than the planned end date.');
                    reject();
                }
                if(formData.value.sjJs && formData.value.sjKs > formData.value.sjJs){
                    ElMessage.error('实际开始时间不能大于实际结束时间/The actual start date cannot be later than the actual end date.');
                    reject();
                }
                resolve(data);
            } else {
                reject();
            }
        })
    })
}

// 打开课程选择弹窗
const vpCourseDialogRef = ref();
const handleChooseCourse = () => {
    vpCourseDialogRef.value.handleOpen({
        selectedId: formData.value.kcid,
    });
}

// 课程选择完成
const handleCourseConfirmed = (course: any) => {
    formData.value.kcid = course.id;
    formData.value.kcbh = course.bh;
    formData.value.kcmcZw = course.mcZw;
    formData.value.kcmcYw = course.mcYw;
    formData.value.fsbs = course.fxbs;
    formData.value.pxjg = course.pxjg;
    if (formData.value.fsbs === 10) {
        formData.value.ks = course.ks;
        formData.value.pxxs = course.pxxs;
        formData.value.ksxs = course.ksxs;
    } else if (formData.value.fsbs === 20) {
        formData.value.ks = course.fxks;
        formData.value.pxxs = course.fxpxxs;
        formData.value.ksxs = course.fxksxs;
    }
}

// 选择教员
const vpUserDialogRef = ref();
const handleChooseTrainer = ()=>{
    vpUserDialogRef.value.handleOpen({
        type: 2, // 1、选择人员t_user 2、选择档案b_p3_001
        multi: false,    // 是否多选
        selected: formData.value.jydaid, // 默认选中人员id
        callback: (data: any) => {
            formData.value.jydaid = data.daid;
            formData.value.jyxm = data.xm;
        }
    });
}

// 选择计划参训人员
const handleChoosePlannedParticipants = () => {
    vpUserDialogRef.value.handleOpen({
        type: 2, // 1、选择人员t_user 2、选择档案b_p3_001
        multi: true,    // 是否多选
        selected: (formData.value.requiredPersons || []).map((trainee) => {return trainee.rydaid}), // 默认选中人员id
        callback: (list: any) => {
            formData.value.requiredPersons = list.map((user: any) => {
                return {
                    rydaid: user.daid,
                    ryxm: user.xm,
                }
            });
        }
    });
}

// 参训人员姓名
const trainingParticipantDesc = computed(() => {
    let desc = '';
    const list = formData.value.requiredPersons;
    if (list && list.length > 0) {
        desc = "已选择"+list.length+"人 " + list.map((trainee)=>{return trainee.ryxm}).join(', ');
    } 
    return desc;
});

// 查看课程
const router = useRouter();
const handleViewCourse = (id: string)=>{
	router.push({
		name: "viewTrainingCourse",
		params: {
			id: id,
		},
	})
}

// 查看人员档案
const handleViewPersonnelFile = (id: string) => {
	router.push({
		name: "viewTrainingPersonnelFile",
		params: {
			id: id,
		},
	})
}

// 选择实际参训人员
const handleChooseActualParticipants = () => {
    vpUserDialogRef.value.handleOpen({
        type: 2, // 1、选择人员t_user 2、选择档案b_p3_001
        multi: true,    // 是否多选
        selected: (formData.value.actualPersons || []).map((trainee) => {return trainee.rydaid}), // 默认选中人员id
        callback: (list: any) => {
            var oldList = formData.value.actualPersons || [];
            var addList = list.filter((obj:any) => {
                return !oldList.some((old:any) => {return old.rydaid === obj.daid});
            }).map((user: any) => {
                console.log(user);
                return {
                    rydaid: user.daid,
                    ryxm: user.xm,
                    dabh: user.dabh,
                    bm: user.bmmc,
                }
            });
            formData.value.actualPersons = formData.value.actualPersons.concat(addList);
        }
    });
}

// 删除实际参训人员
const handleDeleteActualParticipant = (index: number) => {
    formData.value.actualPersons.splice(index, 1);
}

// 是否复训改变
const handleChangeFsbs = () => {
    if (formData.value.kcid) {
        request.get(`/training/course/${formData.value.kcid}`).then((res) => {
            if (formData.value.fsbs === 10) {
                formData.value.ks = res.data.ks;
                formData.value.pxxs = res.data.pxxs;
                formData.value.ksxs = res.data.ksxs;
            } else if (formData.value.fsbs === 20) {
                formData.value.ks = res.data.fxks;
                formData.value.pxxs = res.data.fxpxxs;
                formData.value.ksxs = res.data.fxksxs;
            }
        });
    }
}

defineExpose({
    setData,
    getData,
})

</script>
<style scoped lang="scss">
</style>